package com.systemframework.util;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.systemframework.enums.ResultCodeEnum;

import java.io.Serializable;

/**
 * @author summer
 * @date 2022-10-21 21:20
 **/

public class Result<T> implements Serializable {
    private Integer code;
    private String msg;
    private T data;

    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public Result(Integer code, String msg) {
        this.code = code;
        this.msg = msg;
    }

     

    public Result(Integer code, T data) {
        this.code = code;
        this.data = data;
    }

    public Result(ResultCodeEnum codeEnum) {
        this.code = codeEnum.code();
        this.msg = codeEnum.msg();
    }

    public static <T> Result<T> successResult(String msg, T data) {
        Result<T> result = new Result<T>(ResultCodeEnum.SUCCESS);
        if (StrUtil.isNotEmpty(msg)) {
            result.setMsg(msg);
        }

        if (ObjectUtil.isNotEmpty(data)) {
            result.setData(data);
        }

        return result;
    }

    public static <T> Result<T> successResult(T data) {
        return successResult(ResultCodeEnum.SUCCESS.msg(), data);
    }

    public static <T> Result<T> successResult() {
        return new Result<T>(ResultCodeEnum.SUCCESS);
    }



    public static <T> Result<T> errorResult(String errorMsg) {
        return new Result<T>(ResultCodeEnum.ERROR.code(), errorMsg);
    }

    public static <T> Result<T> errorResult(Integer code,Object data){

        return new Result(code,data);
    }

    public static <T> Result<T> errorResult(ResultCodeEnum enumCode) {
        return new Result<T>(enumCode);
    }
}
